require('../css/play.less')
const baseURL = require('../utils/baseURL.js')

window.onload = function () {
    let str = JSON.parse(sessionStorage.getItem('fragments'))
    let index = 0
    let video = document.querySelector('video')
    let span = document.querySelectorAll('span')
    let name = document.querySelector('.name')

    function render() {
        video.src = baseURL + str[index].videoUrl
        span[0].innerHTML = index + 1
        span[1].innerHTML = str.length
        name.innerHTML = str[index].title
    }
    render()

    // 进度条
    let num
    let progressDom = document.querySelector('.progress');
    function time() {
        let timer = setInterval(function () {
            num = Math.floor(video.currentTime / video.duration * 100) + '%'
            progressDom.style.width = num;
            if (num == '100%') {
                clearInterval(timer)
            }
        }, 100)
    }
    time()

    // 暂停蒙层
    let videos = document.querySelector('.current>video')
    let text = document.querySelector('.text')
    let i = document.querySelectorAll('i')
    let model = document.querySelector('.model')
    function models() {
        let index = 3
        i[1].addEventListener('click', function () {
            // index++
            videos.src = baseURL + str[index].videoUrl
            text.innerHTML = str[index].title
            model.style.display = 'block';
            video.pause()
        })
    }
    models()

    // 继续锻炼和结束锻炼
    let go = document.querySelector('.continue')
    let stop = document.querySelector('.end')

    go.addEventListener('click', function () {
        model.style.display = 'none';
        video.play()

    })

    stop.addEventListener('click', function () {
        location.href = 'motion.html'
        video.pause()
    })

    // 上一个视频
    i[0].addEventListener('click', function () {
        index--
        // console.log(11);
        if (index < 0) {
            index = 0
            common.toast('这已经是第一个视频了', false)
        }
        render()
        models()
    })

    // 下一个视频
    i[2].addEventListener('click', function () {
        index++
        if (index > str.length - 1) {
            index = str.length - 1
            common.toast('这已经是最后一个视频了', false)
        }
        render()

    })

    // 自动下一个
    video.addEventListener('ended', function () {
        i[2].click()
        time()
    })


}
